package git.determinal.template.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

/**
 * @Description
 * @Author determinal
 * @Date 2024/6/24 下午3:09
 */
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        System.out.println("MyBeanFactoryPostProcessor的postProcessBeanFactory方法");
        // 注册BeanDefinition
        BeanDefinition personDefinition = new RootBeanDefinition();
        personDefinition.setBeanClassName("git.determinal.template.dao.impl.PersonDaoImpl");
        // 强转成DefaultListableBeanFactory
        DefaultListableBeanFactory personListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
        personListableBeanFactory.registerBeanDefinition("personDao", personDefinition);
        System.out.println(personDefinition);

        // 修改某个BeanDefinition
        BeanDefinition userServiceDefinition = beanFactory.getBeanDefinition("userService");
        userServiceDefinition.setBeanClassName("git.determinal.template.service.impl.UserServiceImpl");
    }
}
